class Route {
  public query: any
	constructor() {
		this.query = {};
	}
	setQuery(data: any) {
		this.query = data;
	}
}
class Router {
  public routes: any
  public currentPages: Array<any>
  public route: Route
  public type: string
  public to: any
  public from: any
  public routerBefore: any
  public routerAfter: any
	constructor(config: any) {
		this.routes = config.routes;
		this.currentPages = [];
		this.route = new Route();
		console.log(this.route);
		this.type = "";
		this.to = null;
		this.from = null;
	}
	beforeEach(config: any) {
		this.routerBefore = config;
		return config
	}
	afterEach(config: any) {
		this.routerAfter = config;
		return config
	}
	getRouterWithName(name: string){
		let route = null
		for(let i in this.routes){
			if(name == this.routes[i].name){
				route = this.routes[i]
				break;
			}
		}
		if(route == null){
			console.error(name + "路由不存在")
		}
		return route;
	}
	getRouterWithPath(path: string){
		let route = null
		for(let i in this.routes){
			if(path == this.routes[i].path){
				route = this.routes[i]
				break;
			}
		}
		if(route == null){
			console.error(path + "路由不存在")
		}
		return route;
	}
	getPath(path: string, params: any){
		let d = params;
		let _str = '';
		if(d != {}){
			path = path + "?";
			for(let o in d){
				path = path + "&" + o + "=" + d[o];
			}
		}
	}
	jumpFunc(config: any, type: string){
		console.log(config)
		let arr = getCurrentPages()
		if(arr.length > 0){
			this.from = this.getRouterWithPath('/' + arr[arr.length - 1].route);
		}
		this.type = type;
		if(type == "back"){
			if(config >= arr.length){
				this.to = this.routes[0];
				this.type = "replaceAll"
				this.route.setQuery({});
			}else{
				this.to = this.getRouterWithPath('/' + arr[arr.length - 1 - config].route);
				this.to.params = config;
				this.route.setQuery({});
			}
		}else{
			let router = this.getRouterWithName(config.name);
			let path = this.getPath(router.path, config.params);
			this.to = router;
			this.route.setQuery(config.params);
			this.to.params = config.params;
			this.to.allPath = path;
		}
		this.routerBefore(this.to, this.from,this.next.bind(this));
	}
	next(){
		switch(this.type){
			case "back":
				uni.navigateBack({
					delta: this.to.params,
				});
			case "push":
				uni.navigateTo({
				    url: this.to.allPath || this.to.path,
				});
			break;
			case "replace":
				uni.redirectTo({
					url: this.to.allPath || this.to.path,
				});
			break;
			case "replaceAll":
				uni.reLaunch({
					url: this.to.allPath || this.to.path,
				});
			break;
			case "switchTab":
				uni.switchTab({
					url: this.to.allPath || this.to.path,
				});
			break;
		}
		this.routerAfter(this.to, this.from);
	}
	push(config: any) {
		this.jumpFunc(config, "push");
	}
	back(num = 1){
		if(num < 1){
			console.error("back参数不正确")
			return;
		}
		this.jumpFunc(num, "back");
	}
	replace(config: any) {
		this.jumpFunc(config, "replace");
	}
	replaceAll(config: any) {
		this.jumpFunc(config, "replaceAll");
	}
	switchTab(config: any) {
		this.jumpFunc(config, "switchTab");
	}
}
let router = new Router({routes: ROUTER});
//全局路由前置守卫
router.beforeEach((to: any, from: any, next: any) => {
	next();
	// let token = util.getStorage('token') || store.getters.token;
	// if(to.name != "login"){
	// 	if(token){
	// 		next();
	// 	}else{
	// 		next({name: "login"})
	// 	}
	// }else{
	// 	if(token){
	// 		next({name: "home"});
	// 	}else{
	// 		next();
	// 	}
	// }
});
// 全局路由后置守卫
router.afterEach((to: any, from: any) => {
	console.log('跳转结束')
})
export default router;